//
//  UsrDetialViewController.m
//  muscleLife
//
//  Created by xalo on 16/4/28.
//  Copyright © 2016年 HeavenAndHell. All rights reserved.
//

#import "UsrDetialViewController.h"
#import "UsrFileViewController.h"
#import "AccountManagerViewController.h"

@interface UsrDetialViewController ()<UITableViewDelegate, UITableViewDataSource,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, retain)UIImageView* usrImageView;//用户头像图
@property (nonatomic, retain)HFStretchableTableHeaderView* stretchHeaderView;//头背景图

@end

@implementation UsrDetialViewController
//用户头像的懒加载
-(UIImageView *)usrImageView{

    if (!_usrImageView) {
        _usrImageView = [[UIImageView alloc] init];
        [self.tableView addSubview:_usrImageView];
        [_usrImageView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.centerX.mas_equalTo(self.tableView.mas_centerX);
            make.centerY.mas_equalTo(-kControllerWidth/6-32);
            make.height.and.width.equalTo(self.tableView.mas_width).multipliedBy(0.2);
        }];
        [_usrImageView layoutIfNeeded];
    }
    return _usrImageView;
}


#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view layoutIfNeeded];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // Do any additional setup after loading the view.

    [self initStretchHeader];
    [self createHeadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeHeadView:) name:@"ChangeHeadImg" object:nil];
    //获取用户头像
    [self getUsrInfo];
}

-(void)viewWillAppear:(BOOL)animated{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)changeHeadView:(NSNotification*)notification{

    self.usrImageView.image = [notification.userInfo objectForKey:@"img"];
}

//设置头视图头视图
-(void)createHeadView{

    [self.tableView setBackgroundColor:[UIColor lightGrayColor]];
    [[self.usrImageView layer] setMasksToBounds:YES];
    [[self.usrImageView layer] setCornerRadius:self.usrImageView.frame.size.width/2];
    [self.usrImageView layer].borderColor = [[UIColor blackColor] CGColor];
    [self.usrImageView layer].borderWidth = 2.0f;
    [self.usrImageView layer].shadowColor = [[UIColor grayColor] CGColor];
    [self.usrImageView layer].shadowOffset = CGSizeMake(0, 20);
    [self.usrImageView layer].shadowOpacity = 0.8;
    [self.usrImageView layer].shadowRadius = 10.0;
}

-(void)getUsrInfo{
    //获取用户头像
    AVFile* file = [[AVUser currentUser] objectForKey:UIMGKEY];
    if (file) {
        [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {

            UIImage* image = [UIImage imageWithData:data];
            self.usrImageView.image = image;
        }];
    }else{
        self.usrImageView.image = [UIImage imageNamed:kHeadImagePlaceholderFileName];
    }

}

#pragma mark - UItableViewdelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0&&indexPath.row == 0) {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"TitleCell" forIndexPath:indexPath];
        cell.userInteractionEnabled = NO;
        return  cell;
    }else{
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"UsrDetailCell"];

        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UsrDetailCell"];
        }
        NSArray* array = @[@"训练历史",@"我的计划",@"个人资料",@"账号管理",@"夜间模式",@"个人徽章"];
        cell.textLabel.text = [array objectAtIndex:indexPath.section*2 + indexPath.row - 2 ];
        return cell;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {
        return 1;
    }else{
        return 2;
    }

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        return 0;
    }else{
        return 20;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }else{
        UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kControllerWidth, 20)];
        [view setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        return view;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0&&indexPath.row == 0) {
        return 100;
    }else{
        return 60;
    }
}

#pragma mark - tableview的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 2) {
        switch (indexPath.row) {
            case 0:{
                UsrFileViewController* fileViewController = [[UsrFileViewController alloc] initWithNibName:@"UsrFileViewController" bundle:[NSBundle mainBundle]];
                [self.navigationController pushViewController:fileViewController animated:YES];}
                break;
            case 1:{
                AccountManagerViewController* accountManager = [[UIStoryboard storyboardWithName:@"Usr" bundle:nil] instantiateViewControllerWithIdentifier:@"AccountManagerViewController"];
                [self.navigationController pushViewController:accountManager animated:YES];
            }
            default:
                break;
        }
    }
}

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}

- (void)initStretchHeader{
    //背景
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, kControllerHeight/3)];
    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    bgImageView.clipsToBounds = YES;
    bgImageView.image = [UIImage imageNamed:kBackGroundPlaceholderFileName];
    //背景之上的内容
    UIView *contentView = [[UIView alloc] initWithFrame:bgImageView.bounds];
    contentView.backgroundColor = [UIColor clearColor];
    /*
     UIImageView *avater = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
     avater.image = [UIImage imageNamed:@"avater"];
     avater.center = contentView.center;
     [contentView addSubview:avater];
     */

    self.stretchHeaderView = [HFStretchableTableHeaderView new];
    [self.stretchHeaderView stretchHeaderForTableView:self.tableView withView:bgImageView subViews:contentView];

}

#pragma mark - stretchableTable delegate
//头视图缩放
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.stretchHeaderView scrollViewDidScroll:scrollView];
}

- (void)viewDidLayoutSubviews
{
    [self.stretchHeaderView resizeView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
